package com.lingxin.gps.wap.command;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.lingxin.gps.sdk.GpsDeviceApi;
import com.lingxin.gps.sdk.GpsDeviceUtil;
import com.pisces.core.BaseCommand;

import java.util.Iterator;
import java.util.List;

/**
 *
 */
public class CAppIndex extends BaseCommand {

	private String deviceCodes;

	private Long uid;
	private String token;

	@Override
	public void init() throws Exception {
		deviceCodes = getPara("deviceCodes");

		uid = context.getAttr("uid");
		token = context.getAttr("token");
	}

	@Override
	public void doCommand() throws Exception {
		//data.put("deviceInfo", "此处可显示用户关注的信息<br/>如：项目代号：0001ABCD<br/>如：焊接总数：000028");
		data.put("deviceInfo", "");

		JSONObject resData = GpsDeviceApi.userInfo(uid, token);
		data.put("currentUser", resData.getJSONObject("currentUser"));
		//JSONArray groupList = resData.getJSONArray("groupList");

		List<JSONObject> groupList = JSON.parseArray(resData.getString("groupList"), JSONObject.class);
		List<JSONObject> deviceList = GpsDeviceUtil.getDeviceList(groupList, true);
		// 只显示要查看的设备
		if(StrKit.notBlank(deviceCodes)){
			Iterator<JSONObject> iterator = deviceList.iterator();
			JSONObject device;
			while (iterator.hasNext()){
				device = iterator.next();
				if(!deviceCodes.contains(device.getString("deviceCode"))){
					iterator.remove();
				}
			}
		}
		//deviceList = new ArrayList<>();
		data.put("deviceList", deviceList);

	}

}
